package com.byb.server.service.impl;

import com.byb.server.service.ProtectScopeService;
import com.byb.server.domain.ProtectScope;
import com.byb.server.repository.ProtectScopeRepository;
import com.byb.server.repository.search.ProtectScopeSearchRepository;
import com.byb.server.service.dto.ProtectScopeDTO;
import com.byb.server.service.mapper.ProtectScopeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing ProtectScope.
 */
@Service
@Transactional
public class ProtectScopeServiceImpl implements ProtectScopeService{

    private final Logger log = LoggerFactory.getLogger(ProtectScopeServiceImpl.class);

    private final ProtectScopeRepository protectScopeRepository;

    private final ProtectScopeMapper protectScopeMapper;

    private final ProtectScopeSearchRepository protectScopeSearchRepository;
    public ProtectScopeServiceImpl(ProtectScopeRepository protectScopeRepository, ProtectScopeMapper protectScopeMapper, ProtectScopeSearchRepository protectScopeSearchRepository) {
        this.protectScopeRepository = protectScopeRepository;
        this.protectScopeMapper = protectScopeMapper;
        this.protectScopeSearchRepository = protectScopeSearchRepository;
    }

    /**
     * Save a protectScope.
     *
     * @param protectScopeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public ProtectScopeDTO save(ProtectScopeDTO protectScopeDTO) {
        log.debug("Request to save ProtectScope : {}", protectScopeDTO);
        ProtectScope protectScope = protectScopeMapper.toEntity(protectScopeDTO);
        protectScope = protectScopeRepository.save(protectScope);
        ProtectScopeDTO result = protectScopeMapper.toDto(protectScope);
       // protectScopeSearchRepository.save(protectScope);
        return result;
    }

    /**
     *  Get all the protectScopes.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<ProtectScopeDTO> findAll(Pageable pageable) {
        log.debug("Request to get all ProtectScopes");
        return protectScopeRepository.findAll(pageable)
            .map(protectScopeMapper::toDto);
    }

    /**
     *  Get one protectScope by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public ProtectScopeDTO findOne(Long id) {
        log.debug("Request to get ProtectScope : {}", id);
        ProtectScope protectScope = protectScopeRepository.findOne(id);
        return protectScopeMapper.toDto(protectScope);
    }

    /**
     *  Delete the  protectScope by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete ProtectScope : {}", id);
        protectScopeRepository.delete(id);
        //protectScopeSearchRepository.delete(id);
    }

    /**
     * Search for the protectScope corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<ProtectScopeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of ProtectScopes for query {}", query);
        Page<ProtectScope> result = protectScopeSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(protectScopeMapper::toDto);
    }
}
